package com.winchain.chainbill.basic.invoice.rx;

import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.BasInvoiceKeyWord;
import com.winchain.chainbill.basic.invoice.vo.BasInvoiceEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;

@Component
public class BasInvoicePassMsgSubcriber extends OnErrorNotImplementedSubscriber<BasInvoiceEvent> {
	public void onApplicationEvent(BasInvoiceEvent basInvoiceEvent) {
		String template = "MT05000";
		BasInvoiceKeyWord basInvoiceKeyWord = new BasInvoiceKeyWord();
		String applyDate = String.valueOf(basInvoiceEvent.getValue("applyDate"));
		basInvoiceKeyWord.setApplyDate(convertDate(applyDate, "yyyyMMdd", "yyyy-MM-dd"));
		basInvoiceKeyWord.setInvoiceAmt(String.valueOf(basInvoiceEvent.getValue("invoiceAmt")));
		String senderId = CommonDataUtil.getUserId();
		MessageSender.sendMessageByTmpl(senderId, template, basInvoiceKeyWord,
				String.valueOf(basInvoiceEvent.getValue("cusId")));
	}

	private String convertDate(String d, String formStr, String toStr) {
		try {
			Date date = SysDateUtil.getDate(d, formStr);
			SimpleDateFormat sdf = new SimpleDateFormat(toStr);
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
